package car.Postal.Grimentz;

import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.R.integer;



public class stationXMLHandler extends DefaultHandler {

	public int STATION_LIST=10;
	Boolean currentElement = false;
	String currentValue = null;
	public ArrayList<OverlayItem> listeBus = null;
	Integer Idx=-1;
	
	public HashMap<Integer,String> service = null; 
	
	public HashMap<Integer,Integer> produit_Prix = null; 
	public HashMap<Integer,String> produit_Maj = null;
	private Integer serviceId; 
	private Integer carbuId=0;

	NumberFormat format = NumberFormat.getInstance(Locale.ENGLISH);
	
	public ArrayList<OverlayItem> getListeBus() {
		return listeBus;
	}
	
	/*public void setListeStation(StationList StationList) {
		MyXMLHandler.ListeStation = StationList;
	}*/

	/** Called when tag starts ( ex:- <name>AndroidPeople</name>
	 * -- <name> )*/
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {

		currentElement = true;

		if (localName.equals("buss"))
		{
			Idx=-1;
			//zagaz-data   station  station   id  latitude
			/** initialise la structure */
			if(listeBus == null)
				listeBus = new ArrayList<OverlayItem>(3);
			else
				listeBus.clear();
		} else if (localName.equals("bus")) {
				if(attributes.getLength() > 0)
					if( attributes.getLocalName(0).equals("id"))
					{
						Idx++;
						listeBus.add(new OverlayItem());
				//		listeStation.get(Idx).setId(attributes.getValue(0));
					}
		}
		
		else// nous ne devrions pas nous retrouver ici
			return ;
	}

	/** Called when tag closing 
	 * -- </name> )*/
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {

		currentElement = false;

		/** set value */
		if (localName.equalsIgnoreCase("nom"))	//id latitude longitude nom
		{
			listeBus.get(Idx).setNom(currentValue);
		}
		else if (localName.equalsIgnoreCase("latitude"))	//id latitude longitude nom
		{
			//currentValue="46.178256869316";
			Number number;
			try {
				
				number = format.parse(currentValue);
			} catch (ParseException e) {
				e.printStackTrace();
				number=0;
			}    
			double dlat = number.doubleValue();
			listeBus.get(Idx).setLatitude((long)(dlat*10000000));
		}else if (localName.equalsIgnoreCase("longitude"))	//id latitude longitude nom
		{
			//currentValue="7.576478719711";
			Number number;
			try {
				
				number = format.parse(currentValue);
			} catch (ParseException e) {
				e.printStackTrace();
				number=0;
			}    
			double dlat = number.doubleValue();
			listeBus.get(Idx).setLongitude((long)(dlat*10000000));
		}else if (localName.equalsIgnoreCase("longitude"))	//id latitude longitude nom
		{
		//	listeBus.get(Idx).setSpeed(currentValue);
		}else{
			// nous ne devrions pas nous retrouver ici
			return ;
		}
	}

	/** Called to get tag characters ( ex:- <name>AndroidPeople</name>
	 * -- to get AndroidPeople Character ) */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {

		if (currentElement) {
			currentValue = new String(ch, start, length);
			currentElement = false;
		}
	}
}